package com.youbug.mall.workorder_woms.feign.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youbug.common.constant.WomsConstant;
import com.youbug.common.to.OrderItemTo;
import com.youbug.common.to.OrderTo;
import com.youbug.mall.service_ucs.feign.IUcsWorkorderFeignService;
import com.youbug.mall.workorder_woms.entity.WomsWorkOrderInfo;
import com.youbug.mall.workorder_woms.service.IWomsWorkOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UcsWorkorderFeignServiceImpl implements IUcsWorkorderFeignService {

    @Autowired
    private IWomsWorkOrderInfoService womsWorkOrderInfoService;

    @Override
    public void createWorkorder(Long ticketId, List<OrderTo> tos) {

        tos.forEach(order->{

            List<OrderItemTo> itemTos = order.getOrderItemListTemp();

            itemTos.forEach(item->{
                WomsWorkOrderInfo info = new WomsWorkOrderInfo();
                info.setTicketId(ticketId);
                info.setMemberId(order.getMemberId());
                info.setOrderSn(order.getOrderSn());
                info.setSkuId(item.getSkuId());
                info.setProblemTypeName(item.getOrderItemProblemTypeName());
                info.setProblemTypeCode(item.getOrderItemProblemTypeCode());
                info.setProblemDetail(item.getOrderItemProblemDetail());
                info.setRiskLevelCode(item.getRiskLevelCode());
                info.setRiskLevelName(item.getRiskLevelName());
                info.setStatus(WomsConstant.ORDER_INIT);
                boolean update = womsWorkOrderInfoService.update(info, new QueryWrapper<WomsWorkOrderInfo>().eq("ticket_id", ticketId)
                        .eq("order_sn", order.getOrderSn())
                        .eq("sku_id", item.getSkuId()));
                if(!update){
                    womsWorkOrderInfoService.save(info);
                }
            });
        });
    }
}
